﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace WIKID.Common
{
    class Utils
    {
        /// <summary>
        /// Listing all .XML files in a specific folder
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<string> ListingXMLFiles(string path)
        {
             
            List<string> lsFiles = new List<string>();
            if(!string.IsNullOrEmpty(path))
            {
                DirectoryInfo folder = new DirectoryInfo(path);
                try
                {
                    FileInfo[] files = folder.GetFiles("*.xml");
                    foreach (FileInfo file in files)
                    {
                        lsFiles.Add(file.Name.Substring(0,file.Name.Length-4));
                    }
                }
                catch (DirectoryNotFoundException exception)
                {
                    throw exception;
                }
            }
            return lsFiles;
        }

        /// <summary>
        /// Listing all .PNG files in a specific folder
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<string> ListingPNGFiles(string path)
        {

            List<string> lsFiles = new List<string>();
            if (!string.IsNullOrEmpty(path))
            {
                DirectoryInfo folder = new DirectoryInfo(path);
                try
                {
                    FileInfo[] files = folder.GetFiles("*.png");
                    foreach (FileInfo file in files)
                    {
                        lsFiles.Add(file.Name);
                    }
                }
                catch (DirectoryNotFoundException exception)
                {
                    throw exception;
                }
            }
            return lsFiles;
        }

        /// <summary>
        /// Listing all sub folder in a specific folder
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public List<string> ListingSubFolders(string path)
        {

            List<string> lsFolders = new List<string>();
            if (!string.IsNullOrEmpty(path))
            {
                DirectoryInfo mainFolder = new DirectoryInfo(path);
                try
                {
                    DirectoryInfo[] subFolders = mainFolder.GetDirectories();

                    foreach (DirectoryInfo folder in subFolders)
                    {
                        lsFolders.Add(folder.Name);
                    }
                }
                catch (DirectoryNotFoundException exception)
                {
                    throw exception;
                }
            }
            return lsFolders;
        }



        public Boolean IsFolderEmpty(String folderName) 
        { 
            string fileName = @"Accounts\" + folderName;
            if (Directory.GetFiles(fileName).Length > 0) return false;
            else return true;
        }

        /// <summary>
        /// Method to get a hash of a string
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        /// <summary>
        /// Method to verify a hash against a string
        /// </summary>
        /// <param name="input"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        public bool VerifyMd5Hash(String input, String hash)
        {
            
            string hashOfInput = GetMd5Hash(input);
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;
            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

}
